package binding;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.johnson.news.glide.GlideApp;
import com.johnson.news.glide.GlideCircleTransform;
import com.johnson.news.glide.GlideRequest;
import com.johnson.news.glide.GlideRoundRectangleTransform;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseRecyclerAdapter;
import com.youmbe.library.LayoutManager;

import java.util.ArrayList;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;

public class ViewBindingAdapter {

    @BindingAdapter(value = {"setImageUri", "placeholderImageRes", "fitScreenWidth", "noCache"}, requireAll = false)
    public static void setImage(final ImageView image, String url,
                                @DrawableRes final int resId, final boolean fitScreenWidth, final boolean noCache) {
        if (!TextUtils.isEmpty(url)) {
            DrawableTransitionOptions drawableTransitionOptions = DrawableTransitionOptions.withCrossFade(300);
            Context context = image.getContext();
            GlideRequest glideRequest = GlideApp.with(context)//.asBitmap()
                    .load(url);

            glideRequest.error(resId);
            glideRequest.diskCacheStrategy(DiskCacheStrategy.ALL);

            glideRequest.transition(drawableTransitionOptions);

            if(noCache) {
                glideRequest.skipMemoryCache(true);
//                        .diskCacheStrategy(DiskCacheStrategy.NONE);
            }

            glideRequest.into(image);
//            glideRequest.into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
//                    image.setImageBitmap(resource);
//                    if(fitScreenWidth && resource.getWidth() > 0 && resource.getHeight() > 0) {
//                        ViewGroup.LayoutParams layoutParams = image.getLayoutParams();
//                        layoutParams.width = Utils.screenWidth;
//                        layoutParams.height = layoutParams.width * resource.getHeight() / resource.getWidth();
//                        image.setLayoutParams(layoutParams);
//                    }
//                }
//
//                @Override
//                public void onLoadFailed(@Nullable Drawable errorDrawable) {
//                    image.setImageResource(resId);
//                }
//            });
        } else {
            if(resId != 0)
                image.setBackgroundResource(resId);
        }
    }

    @BindingAdapter(value = {"setCircleImageUri", "placeholderImageRes"}, requireAll = false)
    public static void setCircleImage(ImageView image, String url,
                                      @DrawableRes int resId) {//圆角矩形半径
        if (!TextUtils.isEmpty(url)) {
            DrawableTransitionOptions drawableTransitionOptions = DrawableTransitionOptions.withCrossFade(300);
            Context context = image.getContext();
            GlideRequest glideRequest = GlideApp.with(context)
                    .load(url);

            glideRequest.error(resId);

            glideRequest.transform(new GlideCircleTransform());
            glideRequest.transition(drawableTransitionOptions);

            glideRequest.into(image);

        } else {
//            if(resId == 0)//布局中设置了背景的会被覆盖，所以注释掉
//                image.setBackgroundResource(R.mipmap.default_header);
//            else
            if(resId != 0)
                image.setBackgroundResource(resId);
        }
    }

    @BindingAdapter(value = {"setRoundRectangleImageUri", "radius", "placeholderImageRes"}, requireAll = false)
    public static void setRoundRectangleImage(ImageView image, String url, int radius,
                                              @DrawableRes int resId) {//圆角矩形半径
        if (!TextUtils.isEmpty(url)) {
//            DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build();
            DrawableTransitionOptions drawableTransitionOptions = DrawableTransitionOptions.withCrossFade(300);

            Context context = image.getContext();
            GlideRequest glideRequest = GlideApp.with(context)
                    .load(url);
            glideRequest.error(resId);
            glideRequest.transform(new GlideRoundRectangleTransform(Utils.dipTopx(context, radius)));
            glideRequest.transition(drawableTransitionOptions);

            glideRequest.into(image);

        } else {
            if(resId != 0)
                image.setBackgroundResource(resId);
        }
    }

    @BindingAdapter(value = {"setDrawableTop"})
    public static void setDrawableTop(final TextView imageView, String url){
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        imageView.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(resource), null, null);
                    }
                });
    }

    @BindingAdapter(value = {"setHtmlText"})
    public static void setHtmlText(TextView textView, String content){
        if(!TextUtils.isEmpty(content)){
            textView.setText(Html.fromHtml(content));
        }
    }

    @BindingAdapter(value = {"setMiddleLine"})
    public static void setMiddleLine(final TextView textView, String noWork) {
        textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
    }

    @BindingAdapter(value = {"setOnLongClickListener"})
    public static void setOnLongClickListener(View view, View.OnLongClickListener longClickListener) {
        view.setOnLongClickListener(longClickListener);
    }
}
